<template>
  <div class="app-container-component">

    <div class="table-btn">
      <!--<el-button v-if="power" type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>-->

      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
      @fetchData="fetchData"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="80">
        <template slot-scope="scope">
          <el-button
            v-if="power"
            @click.native.prevent="editTeam(scope.row.dataId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            v-if="power"
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <!--
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    -->
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="850px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
        <el-col>
          <el-form-item label="营业执照" size="small" class="item-inline">
            <input type="file" ref='businessLicense' accept="image/*" @change="getBusinessLicense($event)">
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="建筑奖项" size="small" class="item-inline">
            <input type="file" ref='building' accept="image/*" @change="getBuilding($event)">
          </el-form-item>
          <el-form-item label="建筑奖项简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.buildingIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="景观奖项" size="small" class="item-inline">
            <input type="file" ref='landscape' accept="image/*" @change="getLandscape($event)">
          </el-form-item>
          <el-form-item label="景观奖项简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.landscapeIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="市政奖项" size="small" class="item-inline">
            <input type="file" ref='municipal' accept="image/*" @change="getMunicipal($event)">
          </el-form-item>
          <el-form-item label="市政奖项简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.municipalIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="资质证书" size="small" class="item-inline">
            <input type="file" ref='qualification' accept="image/*" @change="getQualification($event)">
          </el-form-item>
          <el-form-item label="资质证书简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.qualificationIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="装饰奖项" size="small" class="item-inline">
            <input type="file" ref='decoration' accept="image/*" @change="getDecoration($event)">
          </el-form-item>
          <el-form-item label="装饰奖项简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.decorationIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="规划奖项" size="small" class="item-inline">
            <input type="file" ref='planning' accept="image/*" @change="getPlanning($event)">
          </el-form-item>
          <el-form-item label="规划奖项简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.planningIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="BIM奖项" size="small" class="item-inline">
            <input type="file" ref='bim' accept="image/*" @change="getBim($event)">
          </el-form-item>
          <el-form-item label="BIM奖项简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.bimIntro"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="荣誉" size="small" class="item-inline">
            <input type="file" ref='honor' accept="image/*" @change="getHonor($event)">
          </el-form-item>
          <el-form-item label="荣誉简介" size="small" class="item-inline">
            <el-input type="text" v-model="teamRow.honorIntro"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="文件查看" clas :visible.sync="imgVisible" width="600px">
      <img class="img-dialog" :src="imgMsg.src" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import { getList, updateAdministrativeForm } from '@/api/administrativedocuments1'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'

  const configs = {
    props: ['colConfig'],
    data() {
      return {
        infoId: '',
        prop: '',
        precent: 0,
        progress: ''
      }
    },
    computed: {
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      downloadFile(row, prop, column) {
        this.row = row
        this.column = column
        this.infoId = row.infoId
        this.prop = prop
        this.$refs.file.click()
      },
      upLoad(upLoad, params, callback1, callback2) {
        upLoad(params, function(progressEvent) { // 原生获取上传进度的事件
          if (progressEvent.lengthComputable) {
            // 属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
            // 如果lengthComputable为false，就获取不到progressEvent.total和progressEvent.loaded
            callback1(progressEvent)
          }
        }).then(res => {
          callback2(res.data)
        })
      },
      fileChanged() {
        const _this = this
        _this.progress = `${_this.row.infoId}${_this.column.id}`
        _this.precent = 0
        const params = new FormData()
        params.append('infoId', this.infoId)
        params.append(`${_this.prop}File`, this.$refs.file.files[0])
        _this.upLoad(updateAdministrativeForm, params, (res) => {
          const loaded = res.loaded
          const total = res.total
          _this.$nextTick(() => {
            _this.precent = (loaded / total) * 100
          })
        }, (res) => {
          _this.$refs.file.value = ''
          _this.progress = ''
          _this.$emit('fetchData')
        })
      }
    },
    template: `
  <el-table-column :label="colConfig.label">
            <input class="file" type="file" @change="fileChanged" ref="file"/>
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >
                    <span v-if="item.component==='filePath'">
                        <div v-if="scope.row[item.prop]">
                            <a v-if="roleType===0 || roleType===1" @click="downloadFile(scope.row,item.prop,scope.column)">上传</a>
                            <a target="_blank" :href="GLOBAL.filePathUrl1+ encodeURI(scope.row[item.prop])">下载</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.infoId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                        <div v-else>
                            <a style="color: red" >X</a>
                            <a v-if="roleType===0 || roleType===1" @click="downloadFile(scope.row,item.prop,scope.column)">上传</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.infoId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                    </span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          {
            label: '固定资产与易耗品', children: [
              { prop: 'assetsRequest', label: '固定资产申请单', width: '160', component: 'filePath' },
              { prop: 'assetsScrap', label: '固定资产报废申请单', width: '160', component: 'filePath' },
              { prop: 'stationery', label: '办公用品领用登记表', width: '160', component: 'filePath' }
            ], component: configs
          },
          {
            label: '后勤', children: [
              { prop: 'expressRequest', label: '快递寄送申请表', width: '160', component: 'filePath' },
              { prop: 'contractHotel', label: '协议酒店信息表', width: '160', component: 'filePath' },
              { prop: 'meetingUse', label: '会议室使用登记表', width: '160', component: 'filePath' },
              { prop: 'administrativeTemporary', label: '行政临时事务登记表', width: '160', component: 'filePath' }
            ], component: configs
          },
          {
            label: '印章', children: [
              { prop: 'sealRequest', label: '印章刻制申请表', width: '160', component: 'filePath' },
              { prop: 'sealLend', label: '印章外借', width: '160', component: 'filePath' },
              { prop: 'sealOver', label: '印章移交表', width: '160', component: 'filePath' },
              { prop: 'sealUse', label: '印章使用申请表', width: '160', component: 'filePath' }
            ], component: configs
          }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增资料',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {},
        visible: false,
        imgVisible: false,
        imgMsg: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    methods: {
      fetchData() {
        // this.searchFormList.pageNum = this.pageNum
        // this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody
          // this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      fileClear() {
        if (this.$refs.businessLicense) {
          this.$refs.businessLicense.value = ''
        }
        if (this.$refs.building) {
          this.$refs.building.value = ''
        }
        if (this.$refs.landscape) {
          this.$refs.landscape.value = ''
        }
        if (this.$refs.municipal) {
          this.$refs.municipal.value = ''
        }
        if (this.$refs.qualification) {
          this.$refs.qualification.value = ''
        }
        if (this.$refs.decoration) {
          this.$refs.decoration.value = ''
        }
        if (this.$refs.planning) {
          this.$refs.planning.value = ''
        }
        if (this.$refs.bim) {
          this.$refs.bim.value = ''
        }
        if (this.$refs.honor) {
          this.$refs.honor.value = ''
        }
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增资料'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.teamRow.isTender = 1
        this.dialogType = 1
        this.fileClear()
      },
      editTeam(dataId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改资料'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.fileClear()
        this.teamRow.dataId = dataId
        const params = {
          dataId: dataId
        }
        selectCompanyFile(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = new FormData()
        if (this.teamRow.businessLicense) {
          params.append('businessLicense', this.teamRow.businessLicense)
        }
        if (this.teamRow.building) {
          params.append('building', this.teamRow.building)
        }
        if (this.teamRow.landscape) {
          params.append('landscape', this.teamRow.landscape)
        }
        if (this.teamRow.municipal) {
          params.append('municipal', this.teamRow.municipal)
        }
        if (this.teamRow.qualification) {
          params.append('qualification', this.teamRow.qualification)
        }
        if (this.teamRow.decoration) {
          params.append('decoration', this.teamRow.decoration)
        }
        if (this.teamRow.planning) {
          params.append('planning', this.teamRow.planning)
        }
        if (this.teamRow.bim) {
          params.append('bim', this.teamRow.bim)
        }
        if (this.teamRow.honor) {
          params.append('honor', this.teamRow.honor)
        }
        if (!this.dialogType) {
          params.append('dataId', this.teamRow.dataId)
        }
        if (this.teamRow.buildingIntro) {
          params.append('buildingIntro', this.teamRow.buildingIntro)
        }
        if (this.teamRow.landscapeIntro) {
          params.append('landscapeIntro', this.teamRow.landscapeIntro)
        }
        if (this.teamRow.municipalIntro) {
          params.append('municipalIntro', this.teamRow.municipalIntro)
        }
        if (this.teamRow.qualificationIntro) {
          params.append('qualificationIntro', this.teamRow.qualificationIntro)
        }
        if (this.teamRow.decorationIntro) {
          params.append('decorationIntro', this.teamRow.decorationIntro)
        }
        if (this.teamRow.planningIntro) {
          params.append('planningIntro', this.teamRow.planningIntro)
        }
        if (this.teamRow.bimIntro) {
          params.append('bimIntro', this.teamRow.bimIntro)
        }
        if (this.teamRow.honorIntro) {
          params.append('honorIntro', this.teamRow.honorIntro)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addCompanyFile(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateCompanyFile(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = new FormData()
        params.append('dataId', scope.row.dataId)
        params.append('isDel', 1)
        updateCompanyFile(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      getBusinessLicense(event) {
        this.teamRow.businessLicense = event.target.files[0]
      },
      getBuilding(event) {
        this.teamRow.building = event.target.files[0]
      },
      getLandscape(event) {
        this.teamRow.landscape = event.target.files[0]
      },
      getMunicipal(event) {
        this.teamRow.municipal = event.target.files[0]
      },
      getQualification(event) {
        this.teamRow.qualification = event.target.files[0]
      },
      getDecoration(event) {
        this.teamRow.decoration = event.target.files[0]
      },
      getPlanning(event) {
        this.teamRow.planning = event.target.files[0]
      },
      getBim(event) {
        this.teamRow.bim = event.target.files[0]
      },
      getHonor(event) {
        this.teamRow.honor = event.target.files[0]
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      }
    },
    mounted() {
      console.log(this.pca)
    },
    computed: {
      roleType() {
        return this.$store.state.user.roleType
      },
      power() {
        return this.roleType === 0 || this.roleType === 1
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container-component {
    height: auto;
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .item-inline {
      display: inline-block;
      width: 400px;
    }
    .img-dialog {
      max-width: 540px;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
